

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../plugins/layui/css/layui.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../plugins/layui/css/style.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../layuiadmin/style/admin.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../plugins/zTree/css/metroStyle/metroStyle.css?v=${.Config.basic.version}">
    <link rel="stylesheet" href="../plugins/zTree/css/metroStyle/ext.css?v=${.Config.basic.version}">
    <link rel="stylesheet" href="//at.alicdn.com/t/c/font_3564966_a6tt4yhnzge.css" media="all">
    <link rel="stylesheet" href="../plugins/toastr/toastr.min.css?v=${.Config.basic.version}">
    <style>
        .layui-form-label {
            width: 60px;
        }
        .layui-form-item .layui-input-inline {
            float: left;
            width: 150px;
            margin-right: 10px;
        }
        .layui-form-item {
            margin-top: 5px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<div class="">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字</label>
                        <div class="layui-input-inline">
                            <input type="text" name="txt_key" lay-verify="" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-sm layui-btn-normal" lay-submit="" lay-filter="btn_search"><i class="layui-icon layui-icon-search"></i>查询</button>
                    </div>

                </div>
            </form>
            <table class="layui-hide" id="table" lay-filter="table"></table>
        </div>
    </div>
</div>



<script type="text/html" id="user_table_toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal layui-hide action-edit" lay-event="add">增加</button>

    </div>
</script>

<script type="text/html" id="table_action_bar">
    <div class="layui-btn-group">
        <button type="button" lay-tips="启动" class="layui-btn  layui-btn-xs" lay-event="start"><i class="layui-icon layui-icon-play"></i></button>
        <button type="button" lay-tips="立即运行一次" class="layui-btn  layui-btn-xs" lay-event="runOnce"><i class="layui-icon layui-icon-right"></i></button>
        <button type="button" lay-tips="停止" class="layui-btn layui-btn-warm  layui-btn-xs" lay-event="stop"><i class="layui-icon layui-icon-circle-dot"></i></button>

        <button type="button" class="layui-btn layui-btn-normal layui-btn-xs layui-hide action-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
        <button type="button" class="layui-btn layui-btn-danger layui-btn-xs layui-hide action-delete" lay-event="del"><i class="layui-icon layui-icon-delete"></i></button>
    </div>
</script>

<script src="../plugins/layui/layui.js"></script>
<script src="../plugins/jquery/jquery-3.6.0.js?v=${.Config.basic.version}"></script>
<script src="../js/utils.js"></script>
<script>
    var table=null;
    var form=null;
    var iconPicker =null;
    var Table=null;
    var sys_roles=null;
    var token = window.localStorage.getItem("token");
    var user_info=JSON.parse(window.localStorage.getItem("user_info"));
    var dictionary=JSON.parse(window.localStorage.getItem("dictionary"));
    var SYS_SCHEDULE_TYPE_ITEMS=[];

    var func_url="{{.func_url}}";
    var actions=[];
    token = 'Bearer ' + token;
    layui.config({
        base: '../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table','form'], function(){
        $ = layui.$;
        form = layui.form;
        table = layui.table;

        $.ajax({
            type: 'GET',
            url: '/Api/User/Actions',
            data: {func_url:func_url},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (data) {
                console.info(data)
                actions=data.data;
                for(var i=0;i<actions.length;i++){
                    $('.action-'+actions[i].action_id).removeClass("layui-hide");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载操作信息失败');
            }
        });

        $.ajax({
            type: 'GET',
            url:"/Api/Dictionary",
            data: {code:"SYS_SCHEDULE_TYPE"},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            success: function(data){
                if(data){
                    var list=data.data;
                    console.info(list);
                    SYS_SCHEDULE_TYPE_ITEMS=list;
                    initTable()
                }
            },
            complete: function(XMLHttpRequest, textStatus) {
                layer.closeAll('loading');
            },
            dataType: "json"
        });




        //监听提交
        form.on('submit(btn_search)', function(data){
            //layer.alert(JSON.stringify(data.field), {title: '最终的提交信息'})
            var value=data.field.txt_key;
            search({where:" concat(name,type) like '%"+value+"%' "});
            return false;
        });



        //头工具栏事件
        table.on('toolbar(table)', function(obj){
            switch(obj.event){
                case 'add':
                    var index = layer.open({
                        type: 2
                        ,title: '添加'
                        ,content: '/Setting/ScheduleAdd'
                        ,maxmin: true
                        ,area: ['90%', '80%']
                        //,offset: '10%'
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#btn_ok");
                            submit.click();
                        }
                    });
                    layer.full(index);
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(table)', function(obj){
            var data = obj.data;
            //console.log(data)
            if(obj.event === 'del'){
                layer.confirm('确认删除'+data.name+"?", function(index){
                    //obj.del();
                    layer.close(index);
                    $.ajax({
                        type: 'DELETE',
                        url: '/Api/Schedule',
                        data: data,
                        beforeSend: function(request) {
                            request.setRequestHeader("Authorization", 'Bearer ' + window.localStorage.getItem("token"));
                        },
                        dataType: "json",
                        success: function (data) {
                            //console.info(data)
                            if(data.code>=0){
                                //layer.msg('成功');
                                layui.table.reload('table'); //重载表格
                                layer.close(index); //再执行关闭
                            }else{
                                layer.alert('失败:'+data.msg);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert('失败');
                        }
                    });
                });
            }
            else if(obj.event === 'edit'){
                var params=decodeURIComponent($.param(data).replace(/\+/g, '%20'));
                //console.info((params));
                var index = layer.open({
                    type: 2
                    ,title: '修改'
                    ,content: '/Setting/ScheduleEdit?'+params
                    ,maxmin: true
                    ,area: ['550px', '550px']
                    ,offset: '100px'
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#btn_ok");
                        submit.click();
                    }
                });
                layer.full(index);
            }
            else if(obj.event === 'start'){
                if(data.status==1){
                    layer.alert("任务已经运行");
                }else{
                    layer.confirm('确认启动'+data.name+"?", function(index){
                        //obj.del();
                        layer.close(index);
                        $.ajax({
                            type: 'POST',
                            url: '/Api/Schedule/StartSchedule',
                            data: data,
                            beforeSend: function(request) {
                                request.setRequestHeader("Authorization", 'Bearer ' + window.localStorage.getItem("token"));
                            },
                            dataType: "json",
                            success: function (data) {
                                //console.info(data)
                                if(data.code>=0){
                                    //layer.msg('成功');
                                    layui.table.reload('table'); //重载表格
                                    layer.close(index); //再执行关闭
                                }else{
                                    layer.alert('失败:'+data.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert('失败');
                            }
                        });
                    });
                }
            }
            else if(obj.event === 'stop'){
                if(data.status==0){
                    layer.alert("任务已经停止");
                }else{
                    layer.confirm('确认停止'+data.name+"?", function(index){
                        //obj.del();
                        layer.close(index);
                        $.ajax({
                            type: 'POST',
                            url: '/Api/Schedule/StopSchedule',
                            data: data,
                            beforeSend: function(request) {
                                request.setRequestHeader("Authorization", 'Bearer ' + window.localStorage.getItem("token"));
                            },
                            dataType: "json",
                            success: function (data) {
                                //console.info(data)
                                if(data.code>=0){
                                    //layer.msg('成功');
                                    layui.table.reload('table'); //重载表格
                                    layer.close(index); //再执行关闭
                                }else{
                                    layer.alert('失败:'+data.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert('失败');
                            }
                        });
                    });
                }
            }
            else if(obj.event === 'runOnce'){
                layer.confirm('确认运行一次 '+data.name+"?", function(index){
                    //obj.del();
                    layer.close(index);
                    $.ajax({
                        type: 'POST',
                        url: '/Api/Schedule/RunOnceSchedule',
                        data: data,
                        beforeSend: function(request) {
                            request.setRequestHeader("Authorization", 'Bearer ' + window.localStorage.getItem("token"));
                        },
                        dataType: "json",
                        success: function (data) {
                            //console.info(data)
                            if(data.code>=0){
                                //layer.msg('成功');
                                layui.table.reload('table'); //重载表格
                                layer.close(index); //再执行关闭
                            }else{
                                layer.alert('失败:'+data.msg);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert('失败');
                        }
                    });
                });
            }
        });

    });

    function initTable(){
        Table = table.render({
            elem: '#table'
            //,data:data
            ,url:'/Api/Schedule'
            ,height: 'full-80'
            ,method:'GET'
            ,headers : {'Authorization': 'Bearer '+window.localStorage.getItem("token")}
            ,where: {}
            ,page: false
            ,limit:30
            ,toolbar: '#user_table_toolbar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: []
            ,title: '用户数据表'
            ,even:false
            ,cols: [[
                {field:'id', title:'id',hide:true}
                ,{field:'name', title:'名称',sort: true, width: 150}
                ,{field:'gcron', title:'表达式',sort: true, align:'center',width: 120}
                ,{field:'type', title:'类型',sort: true, width: 80,templet: function(d){
                        var html="";
                        var matchs = $.grep(SYS_SCHEDULE_TYPE_ITEMS,function (item) {
                            return item.data_value == d.type;
                        });
                        if(matchs.length>0){
                            html=matchs[0].data_text;
                        }
                        return html;
                    }}
                ,{field:'status', title:'运行中', width: 80, sort: false,align:'center',templet: function(d){
                        return d.status===1?"<i class=\"layui-icon layui-icon-play layui-text-green \"></i>":"<i class=\"layui-icon layui-icon-circle-dot layui-text-red \"></i>";
                    }}
                ,{field:'singleton', title:'单例', width: 60, sort: false,align:'center',templet: function(d){
                        return d.singleton===1?"<i class=\"layui-icon layui-icon-note layui-text-green \"></i>":"<i class=\"layui-icon layui-icon-close layui-text-red \"></i>";
                    }}
                //,{field:'max_run_count', title:'允许次数', width: 80, sort: true,align:'center'}
                ,{field:'run_count', title:'运行次数', width: 120, sort: true,align:'center'}
                ,{field:'schedule_log_err_count', title:'错误日志', width: 120, sort: true,align:'center',templet: function(d){
                        return d.schedule_log_err_count===0?"":"<a href='#' onclick='openScheduleLog("+d.id+")'><span class=\"layui-badge\">"+d.schedule_log_err_count+"</span></a>";
                    }}
                //,{field:'begin_run_time', title:'开始时间', width: 180, sort: true,align:'center'}
                //,{field:'end_run_time', title:'结束时间', width: 180, sort: true,align:'center'}
                //,{field:'next_run_time', title:'下次运行时间', width: 160, sort: true,align:'center'}
                ,{field:'last_run_time', title:'最近运行时间', width: 180, sort: true,align:'center'}
                ,{title:'操作', toolbar:'#table_action_bar', width: 200, sort: true}
            ]]
            ,done: function(res, curr, count){
                for(var i=0;i<actions.length;i++){
                    $('.action-'+actions[i].action_id).removeClass("layui-hide");
                }
            }
        });
    }

    function openScheduleLog(id) {
        var index = layer.open({
            type: 2
            ,title: '详细日志'
            ,content: '/log/schedule_log?status=0&id='+id
            ,maxmin: true
            ,area: ['900px', '500px']
            ,offset: '20px'
        });
        //layer.full(index);
        //layer.iframeAuto(index)
    }
    function search(params) {
        Table.reload({
            where: params
        });
    }


</script>
<script src="../js/check_screen.js?v=${.Config.basic.version}"></script>

</body>
</html>

